home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / x / animutil / xanim229.lha / xanim / xanim_x11.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-08  |  8.3 KB  |  341 lines

  1.  
  2. /*
  3.  * xanim_x11.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed
  9.  * without fee provided that this copyright notice is preserved 
  10.  * intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include <X11/Xlib.h>
  20. #include <X11/Intrinsic.h>
  21. #include <X11/StringDefs.h>
  22. #include <X11/Shell.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/signal.h>
  26. #include <sys/times.h>
  27. #include <memory.h>
  28. #include <ctype.h>
  29. #include "mytypes.h"
  30. #include "xanim.h"
  31. #include "xanim_x11.h"
  32.  
  33. void xanim_expose();
  34. void xanim_key();
  35. void xanim_quit();
  36. void xanim_events();
  37. void X11Setup();
  38.  
  39. /*********************************** X11 stuff */
  40. Display       *theDisp;
  41. Visual        *theVisual;
  42. Colormap       theCmap;
  43. Window         mainW;
  44. GC             theGC;
  45. XImage        *theImage;
  46. XColor         defs[256];
  47. /******************************** Xt stuff */
  48. XtAppContext  theContext;
  49.  
  50. #define ACTIONTABLE_SIZE 4
  51. XtActionsRec actionTable[] = {
  52.         {"Expose", xanim_expose},
  53.         {"Quit", xanim_quit},
  54.         {"Key", xanim_key},
  55.         {"Go", ShowAnimation}
  56. };
  57.  
  58. static struct _resource {
  59.   int anim;
  60. } resource;
  61.  
  62. #define offset(field)   XtOffset(struct _resource *, field)
  63.  
  64. XtResource application_resources[] = {
  65.   {"anim", "Anim", XtRBoolean, sizeof (Boolean),
  66.      offset(anim), XtRString, "False" },
  67. };
  68.  
  69. String   Translation =
  70. "<Expose>:Expose()\n\
  71. <Key>:Key()";
  72.  
  73.  
  74. void X11_Pre_Setup()
  75. {
  76.  int i,vis_num;
  77.  XVisualInfo *vis;
  78.  
  79.  XtToolkitInitialize();
  80.  theContext = XtCreateApplicationContext();
  81.  XtAppAddActions(theContext, actionTable, ACTIONTABLE_SIZE);
  82.  
  83.  i = 0;
  84.  theDisp = XtOpenDisplay(theContext, NULL, "xanim", "XAnim",NULL,0,&i,NULL);
  85.  if (theDisp==NULL) 
  86.  {
  87.   TheEnd1("Unable to open display\n"); 
  88.  }
  89.  
  90.  x11_depth = DefaultDepth(theDisp, DefaultScreen(theDisp));
  91.  
  92.  theVisual = DefaultVisual(theDisp, DefaultScreen(theDisp));
  93.  x11_class = theVisual->class;
  94.  if ( (x11_class == PseudoColor) && (x11_depth <=8) && (x11_depth >=1) )
  95.  {
  96.   x11_bytes_pixel = 1;
  97.   x11_cmap_flag = TRUE;
  98.   x11_display_type = PSEUDO_COLOR_8BIT;
  99.  }
  100.  else if ( (x11_class == TrueColor) && (x11_depth == 24) )
  101.       {
  102.        x11_bytes_pixel = 4;
  103.        x11_cmap_flag = FALSE;
  104.        x11_display_type = TRUE_COLOR_24BIT;
  105.       }
  106.       else /* not yet supported */
  107.       {
  108.        fprintf(stderr,"Unsupported Visual class=%ld depth=%ld \n",
  109.         x11_class,x11_depth);
  110.        TheEnd();
  111.       }
  112.  
  113.  x11_cmap_size = DisplayCells(theDisp, DefaultScreen(theDisp));
  114.  
  115.  
  116. #ifdef X11_VERBOSE
  117.  fprintf(stderr,"Class = %ld depth=%ld bytes_pixel=%lx\n",
  118.         x11_class,x11_depth,x11_bytes_pixel);
  119.  fprintf(stderr,"x11_cmap_size = %ld\n",x11_cmap_size);
  120. #endif
  121.  
  122.  
  123. #ifdef X11_VERBOSE
  124.  vis = XGetVisualInfo (theDisp, VisualNoMask, NULL, &vis_num);
  125.  if ((vis == NULL) || (vis_num == 0) )
  126.  {
  127.   TheEnd1("X11: Couldn't get any Visuals\n");
  128.  }
  129.  else
  130.  {
  131.   for(i=0;i<vis_num;i++)
  132.   {
  133.    fprintf(stderr,"visual %ld) depth= %ld  class= %ld  ",
  134.      i, vis[i].depth, vis[i].class, vis[i].colormap_size );
  135.    fprintf(stderr,"%ld) class = %ld\n",i,vis[i].class);
  136.    fprintf(stderr,"%ld) cmap sz = %ld\n",i,vis[i].colormap_size);
  137.   }
  138.  }
  139.  XFree(vis);  
  140. #endif
  141.  
  142. }
  143.  
  144. /*
  145.  * Setup X11 Display, Window and Toolkit
  146.  */
  147. void X11_Setup(max_imagex,max_imagey,startx,starty)
  148. int max_imagex,max_imagey;
  149. int startx,starty;
  150. {
  151.  int i,off;
  152.  Widget wg;
  153.  int n;
  154.  Arg arglist[20];
  155.  
  156.  
  157.  
  158.  n = 0;
  159.  XtSetArg(arglist[n], XtNwidth, startx); n++;
  160.  XtSetArg(arglist[n], XtNheight, starty); n++;
  161.  XtSetArg(arglist[n], XtNmaxWidth, max_imagex); n++;
  162.  XtSetArg(arglist[n], XtNmaxHeight, max_imagey); n++;
  163.  wg = XtAppCreateShell("xanim", "XAnim", applicationShellWidgetClass, 
  164.                         theDisp, arglist, n);
  165.  
  166.  XtGetApplicationResources (wg, &resource, application_resources,
  167.                             XtNumber(application_resources), NULL, 0);
  168.  
  169.  XtRealizeWidget(wg);
  170.  XtOverrideTranslations(wg, XtParseTranslationTable(Translation));
  171.  
  172.  mainW = XtWindow(wg);
  173.  
  174.  theImage = XCreateImage(theDisp,DefaultVisual(theDisp,DefaultScreen(theDisp)),
  175.                             x11_depth,ZPixmap,0,0,max_imagex,max_imagey,
  176.                 x11_bytes_pixel,(x11_bytes_pixel * max_imagex));
  177.  
  178.  theCmap   = DefaultColormap(theDisp, DefaultScreen(theDisp));  
  179.  theGC     = DefaultGC(theDisp,DefaultScreen(theDisp));
  180.  
  181.  if (x11_cmap_flag == TRUE)
  182.  {
  183.    for(i=0;i<x11_cmap_size;i++) 
  184.    {
  185.       defs[i].pixel = i;
  186.       defs[i].flags = DoRed | DoGreen | DoBlue;
  187.    }
  188.    XQueryColors(theDisp,DefaultColormap(theDisp, DefaultScreen(theDisp)),
  189.                 defs,x11_cmap_size);
  190.  
  191.    off = x11_cmap_size - imagec;
  192.    for(i=0;i<imagec;i++)
  193.    {
  194.     defs[off+i].pixel = off+i;
  195.     defs[off+i].red   = cmap[i].red   << 8;
  196.     defs[off+i].green = cmap[i].green << 8;
  197.     defs[off+i].blue  = cmap[i].blue  << 8;
  198.     defs[off+i].flags = DoRed | DoGreen | DoBlue;
  199.    }
  200.    theCmap = XCreateColormap(theDisp,mainW,
  201.                              DefaultVisual(theDisp, DefaultScreen(theDisp)),
  202.                              AllocAll);
  203.    if (theCmap==0) 
  204.    {
  205.     fprintf(stderr,"cmap==0 error\n");
  206.    }
  207.    XStoreColors(theDisp,theCmap,defs,x11_cmap_size);
  208.    XInstallColormap(theDisp,theCmap);
  209.    XSetWindowColormap(theDisp, mainW, theCmap);
  210.  }
  211.  
  212.  XSelectInput(theDisp, mainW, ExposureMask | KeyPressMask);
  213.  
  214.  XMapWindow(theDisp,mainW);
  215.  XFlush(theDisp);
  216. }
  217.  
  218. void xanim_expose(wg, event, str, np)
  219.         Widget          wg;
  220.         XExposeEvent   *event;
  221.         String         *str;
  222.         int            *np;
  223. {
  224.  if (   ( (anim_flags & ANIM_CYCLE) && cycle_on_flag && cycle_wait)
  225.      || ( (anim_cnt == 1) && cycle_wait)
  226.     )
  227.  {
  228.   XPutImage(theDisp,mainW,theGC,theImage,0,0,0,0,imagex,disp_y);
  229.  }
  230. }
  231.  
  232. void xanim_key(wg, event, str, np)
  233. Widget          wg;
  234. XKeyEvent      *event;
  235. String         *str;
  236. int            *np;
  237. {
  238.         char            buff[10];
  239.         KeySym          ks;
  240.         XComposeStatus  st;
  241.  if (event->type == KeyPress)
  242.  {
  243.         XLookupString(event, buff, 10, &ks, &st);
  244.     switch(buff[0])
  245.     {
  246.      case 'q':
  247.      case 'Q': TheEnd();
  248.            break;
  249.      case 'g':
  250.          case 'G': anim_flags &= (~ANIM_CYCLE);
  251.            break;
  252.          case 's': 
  253.          case 'S': 
  254.            if (cycle_on_flag) cycle_on_flag = 0;
  255.            else  cycle_on_flag = 1;
  256.            break;
  257.     
  258.          case 'r': 
  259.          case 'R': 
  260.            {
  261.             int j,c_off;
  262.  
  263.             c_off = x11_cmap_size-imagec;
  264.                     for(j=0;j<imagec;j++)
  265.                     {
  266.                      defs[j].pixel = j+c_off;
  267.                      defs[j].red   = cmap[j+c_off].red   << 8;
  268.                      defs[j].green = cmap[j+c_off].green << 8;
  269.                      defs[j].blue  = cmap[j+c_off].blue  << 8;
  270.                      defs[j].flags = DoRed | DoGreen | DoBlue;
  271.                     }
  272.                     XStoreColors(theDisp,theCmap,defs,imagec);
  273.              XFlush(theDisp);
  274.            }
  275.            break;
  276.          case '.': 
  277.            if (anim_running==TRUE) anim_running = FALSE;
  278.               else
  279.            {
  280.             anim_forward_flag = TRUE;
  281.             Step_Frame_Next();
  282.                   XtAppAddTimeOut(theContext, 1, ShowAction, NULL);
  283.            }
  284.            break;
  285.          case ',': 
  286.            if (anim_running==TRUE) anim_running = FALSE;
  287.               else
  288.            {
  289.             anim_forward_flag = FALSE;
  290.             Step_Frame_Prev();
  291.                   XtAppAddTimeOut(theContext, 1, ShowAction, NULL);
  292.            }
  293.            break;
  294.          case '>': 
  295.            if (anim_running==TRUE) anim_running = FALSE;
  296.               else
  297.            {
  298.             anim_forward_flag = TRUE;
  299.             Step_File_Next();
  300.                   XtAppAddTimeOut(theContext, 1, ShowAction, NULL);
  301.            }
  302.            break;
  303.          case '<': 
  304.            if (anim_running==TRUE) anim_running = FALSE;
  305.               else
  306.            {
  307.             anim_forward_flag = TRUE;
  308.             Step_File_Prev();
  309.                   XtAppAddTimeOut(theContext, 1, ShowAction, NULL);
  310.            }
  311.            break;
  312.          case ' ': 
  313.            if (anim_running==TRUE) anim_running = FALSE;
  314.               else
  315.            {
  316.             anim_running=TRUE;
  317.             Step_Action_Next();
  318.                   XtAppAddTimeOut(theContext, 1, ShowAction, NULL);
  319.            }
  320.            break;
  321.          case 'm': 
  322.            anim_forward_flag = (anim_forward_flag==TRUE)?FALSE:TRUE;
  323.            break;
  324.     }
  325.  }
  326. }
  327.  
  328. void xanim_quit(wg, event, str, np)
  329. Widget          wg;
  330. XKeyEvent      *event;
  331. String         *str;
  332. int            *np;
  333. {
  334.  TheEnd(); 
  335. }
  336.  
  337. void xanim_events()
  338. {
  339.  XtAppMainLoop(theContext);
  340. }
  341.